<?php
//require section
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_session.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_outside_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_movies.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'general_functions.php';

$title = 'Now Showing';

if (!empty($_POST)) {

}
?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="ts-body-container">
            <div class="ts-wrapper">
                <!--<div class="ui grid">-->
                <div class="padding-top-large row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php';
                    ?>
                    <div class="ui segment breadcrumb" style="margin-top: 25px; margin-left: 20px;">
                        <a class="section" href="index.php">Home</a>
                        <i class="right arrow icon divider"></i>
                        <a class="section" href="now_showing.php">Movies</a>
                        <i class="right arrow icon divider"></i>
                        <div class="active section">Now Showing</div>
                    </div>
                </div>
                <div class="ui grid">
                    <div class="row">
                        <?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'book_ticket.php'; ?>
                        <div class="twelve wide column">
                            <h1>Now Showing</h1>
                            <div class="ui orange segment">
                                <div class="ui stackable items">
                                    <?php
                                    $list = api_movies::get_all_movies_with_now_showing();

                                    $current_page = 1;
                                    if (!empty($_GET['p'])) {
                                            $current_page = $_GET['p'];
                                    }

                                    $list_total = $list;
                                    $page_size = 9;
                                    $lib_pager = new lib_pager($list_total, $page_size);
                                    $total_page = $lib_pager->get_total_page();
                                    $current_page_list = $lib_pager->get_current_page_list($current_page);

                                    if (empty($current_page_list) || count($current_page_list) == 0) {
                                        echo "No movie found.";
                                    } else {
                                        echo '<div class="row">';
                                        foreach ($current_page_list as $key => $item) {
                                            if ($key > 0 && $key % 3 == 0) {
                                                echo '</div>';
                                                echo '<div class="row">';
                                            }
                                            show_movie($item);
                                        }
                                        echo '</div>';
                                    }?>
                                </div>
                            </div>
                            <div class="ts-center">
                                <div class="ui pagination menu">
                                    <?php
                                    if (!empty($total_page) && count($total_page) > 0) {?>
                                        <a class="item" <?php if($current_page<=1){echo "onclick='return false'";} ?>
                                           href="?p=<?php echo $current_page - 1;?>">
                                            <i class="left arrow icon"></i> Previous
                                        </a>
                                        <?php
                                        for ($page = 1; $page <= $total_page; $page++) { ?>
                                        <a class="item" href="?p=<?php echo $page; ?>"><?php echo $page; ?></a>
                                        <?php
                                        }?>
                                        <a class="item" <?php if($current_page>=$total_page){echo "onclick='return false'";} ?>
                                           href="?p=<?php echo $current_page + 1;?>">
                                            Next <i class="icon right arrow"></i>
                                        </a>
                                        <?php
                                    } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
                    ?>
                </div>
                <!--</div>-->
            </div>
        </div>
    </body>
</html>